import { login, getInfo, logout } from '@/api/user'
import { getBrand, getChannel, getGroup } from '@/api/comdata'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    userInfo: '',
    brand: [],
    channel: {},
    group: [],
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERINFO: (state, info) => {
    state.userInfo = info
  },
  SET_BRAND: (state, data) => {
    state.brand = data
  },
  SET_CHANNEL: (state, data) => {
    state.channel = data
  },
  SET_GROUP: (state, data) => {
    state.group = data
  },
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ phone: username.trim(), password: password })
        .then(response => {
          // const { data } = response
          // commit('SET_NAME', data.username)
          // commit('SET_USERTYPE', data.u_type)
          // commit('SET_CID', data.boss_id)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then(response => {
          const { data } = response
          // test
          data.permission_id = [
            15,
            30,
            31,
            32,
            34,
            37,
            38,
            101,
            102,
            103,
            139,
            141,
            142,
            143,
            144,
            145,
          ]
          // test
          commit('SET_USERINFO', data)
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('RESET_STATE')
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // brand data
  getBrand({ commit, state }) {
    return new Promise((resolve, reject) => {
      getBrand()
        .then(response => {
          const { data } = response
          commit('SET_BRAND', data)
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // channel data
  getChannel({ commit, state }) {
    return new Promise((resolve, reject) => {
      getChannel()
        .then(response => {
          const { data } = response
          const channelOb = {}
          data.forEach((value, index) => {
            channelOb[value.cid] = value.channel_name
            if (index >= data.length - 1) {
              commit('SET_CHANNEL', { list: data, ob: channelOb })
            }
          })
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // group data
  getGroup({ commit, state }) {
    return new Promise((resolve, reject) => {
      getGroup()
        .then(response => {
          const { data } = response
          const groupOb = {}
          commit('SET_GROUP', data)
          data.forEach((value, index) => {
            groupOb[value.id] = value.full_name
            if (index >= data.length - 1) {
              commit('SET_GROUP', { list: data, ob: groupOb })
            }
          })
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // set token
  setToken({ commit }, token) {
    return new Promise(resolve => {
      commit('SET_TOKEN', token)
      setToken(token)
      resolve()
    })
  },
  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
